package com.fileshare.web.controller.index.api;

import com.fileshare.web.controller.index.BaseController;
import com.fileshare.web.dto.Bo.ArticleBo;
import com.fileshare.web.dto.Result;
import com.fileshare.web.dto.Vo.ArticleVo;
import com.fileshare.web.dto.Vo.FileResourceVo;
import com.fileshare.web.service.ArticleService;
import com.fileshare.web.service.FileResourceService;
import com.fileshare.web.service.UploadService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 文章
 */
@RestController
@RequestMapping("/api/article")
public class ArticleApiController extends BaseController {
    @Resource
    private ArticleService articleService;
    @Resource
    private FileResourceService fileResourceService;
    @Resource
    private UploadService uploadService;

    @PostMapping
    public Result<ArticleVo> createArticle(HttpServletRequest request, @RequestBody ArticleBo articleBo) {
        ArticleVo articleVo = articleService.createArticle(request, articleBo);
        return Result.success().with(articleVo);
    }

    @PostMapping("/image")
    public Result<String> createArticle(@RequestParam("file") MultipartFile file) {
        String url = uploadService.uploadFile(file);
        return Result.success().with(url);
    }

    @GetMapping("/resources/{articleSeq}")
    public Result<List<FileResourceVo>> getArticleResources(@PathVariable String articleSeq) {
        List<FileResourceVo> articleResources = fileResourceService.getArticleResources(articleSeq);
        return Result.success().with(articleResources);
    }
}
